/**
 * Created by user on 2018/11/19.
 */

const sequelize = require('sequelize')
const { Model } = sequelize

const checkIsModel = r => r instanceof Model
const checkIsArray = r => Array.isArray(r)


const formatSequelizeResult = (result) => {
    if (!result) {
        return result
    }
    if (typeof result !== 'object') {
        return result
    }
    if (checkIsArray(result)) {
        return result.map(r => formatSequelizeResult(r))
    }
    if (checkIsModel(result)) {
        return formatSequelizeResult(result.dataValues)
    }
    let o = {}
    for (let key in result) {
        let v = result[key]
        if (checkIsModel(v)) {
            o[key] = formatSequelizeResult(v.dataValues)
        } else if (checkIsArray(v)) {
            o[key] = formatSequelizeResult(v)
        } else {
            o[key] = v
        }
    }
    return o
}


module.exports = {
    formatSequelizeResult
}